// JavaScript Document
function $(str){
	return new query(str);	
}
function query(_Id){
	this.init(_Id);
}
query.prototype = {
	init:function(s){
		if(!s){return this;}
		if(s.nodeType){
			this[0]=s;
			this.length = 1;
			return this;
		}
		s = s.split(/\s/);
		this.find(document,0,s);
	},
	find:function(ele,nSub,tar){
		if(ele==null)return this;
		if(ele.constructor != Array){
			ele = [ele];
		}
		if(tar[nSub]){
			
			var reg = /^#(.+)$|^\.(.+)$|^([\w\d]+)$/.exec(tar[nSub]);
			if(reg[1]){
				ele = document.getElementById(reg[1]);
			}else{
				var m = ' '+reg[2]+' ';
				var eles = new Array();
				
				for(var i=0; i<ele.length; i++){
					if(reg[2]){
						var s = ele[i].getElementsByTagName('*');
						for(var j=0; j<s.length; j++){
							if((' '+s[j].className+' ').indexOf(m)>=0){
								eles.push(s[j]);
							}
						}
					}else if(reg[3]){
						var s = ele[i].getElementsByTagName(reg[3]);
						for(var j=0; j<s.length; j++){
							eles.push(s[j]);
						}
					}
				}
				ele = eles;
				if(ele.length ==0)return this;
			}
			nSub++;
			this.find(ele,nSub,tar);
		}else{
			return this.listArr(ele);
		}
	},
	css:function(str,target){
		str = str.split(/:|;/);
		for(var i=0; i<str.length; i=i+2){
			if(target){
				this.setCss(str[i],str[i+1],target);
			}else{
				for(var j=0; j<this.length; j++){
					this.setCss(str[i],str[i+1],this[j]);
				}
			}
		}
		return this;
	},
	setCss:function(pro,val,ele){
		pro = pro.replace('-','');
		for(var k in ele.style){
			if(pro.toLowerCase()==k.toLowerCase()){
				ele.style[k] = val;
			}
		}
	},
	listArr:function(arr){
		for(var i=0; i<arr.length; i++){
			this[i] = arr[i];
		}
		this.length = arr.length;
		return this;
	},
	bindEvent:function(type,fn,elem){
		if(elem.attachEvent){
			var typeRef = "_" + type;
			if(!elem[typeRef]){
				elem[typeRef] = [];
			}
			for(var i in elem[typeRef]){
				if(elem[typeRef][i] == fn){
					return;
				}
			}
			elem[typeRef].push(fn);
			elem["on"+type] = function(){
				for(var i in this[typeRef]){
					this[typeRef][i].apply(this,arguments);
				}
			}   
		}else{
			elem.addEventListener(type,fn,false);
		}
	},
//	bindEvent:function(type,fn,elem){
//		if(elem.attachEvent){
//        elem.attachEvent("on"+type,function(){
//            fn.apply(elem,arguments);
//			});
//		}else{
//			elem.addEventListener(type,fn,false);
//		}
//	},
	removeEvent:function(type,fn,elem){
		if(elem.detachEvent){
			if(elem["_"+type]){
				for(var i in elem["_"+type]){
					if(elem["_"+type][i] == fn){
						elem["_"+type].splice(i,1);
						break;
					}
				}
			}
		}else{
			elem.removeEventListener(type,fn,false);
		}
	},
	bind:function(type,fn,elem){
		if(elem){
			this.bindEvent(type,fn,elem);
		}else{
			for(var j=0; j<this.length; j++){
				this.bindEvent(type,fn,this[j]);
			}
		}
		return this;
	},
	unBind:function(type,fn,elem){
		if(elem){
			this.removeEvent(type,fn,elem);
		}else{
			for(var j=0; j<this.length; j++){
				this.removeEvent(type,fn,this[j]);
			}
		}
		return this;
	}
//	bind:function bind(type,func){
//		for(var i=0; i<this.length; i++){
//			if (this[i].addEventListener){
//				this[i].addEventListener(type, func, false);
//			}else if (this[i].attachEvent){
//				this[i].attachEvent("on" + type, func);
//			}else{ this[i]["on" + type] = func;}
//		}
//		return this;
//	},
//	unBind:function unbind(type,func){
//		for(var i=0; i<this.length; i++){
//			if(this[i].removeEventListener){//for DOM;
//				this[i].removeEventListener(type,func, false);
//			}else if(this[i].detachEvent){
//				this[i].detachEvent("on"+type,func);
//			}else{
//				this[i]["on"+type]=null;
//			}
//		}
//		return this;
//	}
}